<?
	require_once "../../_func/__Global.php";
	require_once "../../_func/__Local.php";
	require_once "../../_func/__MysqlFunc.php";
	require_once "../../_func/_BoardFunc.php";
	
	cacheControl(0);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>:: do ::</title>
</head>
<body>
<?	
	
	
	//get parameter
	$b = $param->getString('b');
	$page = $param->getNumber('page');
	$qtype = $param->getString('qtype');
	$query = $param->getString('query');
	$mode = $param->getString('m');
	$no = $param->getNumber('n');
	$menu1 = $param->getString('menu1');
	$menu2 = $param->getString('menu2');
	

	//게시판 유무 체크
	TalkBoard_IsValidBid($b);

	$basic_param = array(
		'b'				=> $b,
		'page'			=> $page,
		'qtype'			=> $qtype,
		'query'			=> $query,
		'menu1'			=> $menu1,
		'menu2'			=> $menu2,	
	);
	
	
	//원글, 답글
	if ( ($mode == "w") || ($mode == "r") )
	{
		$subject				= $param->getString('subject');
		$writer_id				= GetCurrentUser();
		$writer_name			= $param->getString('writer_name');
		$content				= $param->getString('content');

		if ( ($mode == "r")  && ( $b == "bn") )
		{
			jserror("답글작성이 허용되지 않습니다.","document.location='about:blank'");
			return;
		}		
		
		if ( ($mode == "r")  && ( $b != "bn") ) 
		{
			$ono					= $param->getNumber('no');
			if (!isset($ono) || $ono == "")
				jserror("글 번호가 입력되지 않았습니다.");
		}
		
		if ($writer_name == "")
		{
			jserror("작성자가 입력되지 않았습니다.","document.location='about:blank'");
			return;
		}

		if ($subject == "")
		{
			jserror("제목이 입력되지 않았습니다.","document.location='about:blank'");
			return;
		}

		$article_arr = array(
			'writer_id'				=> $writer_id,
			'writer_name'			=> $writer_name,
			'subject'					=> $subject,
			'content'				=> $content,
		);
		
		if ( ($mode == "r")  && ( $b != "bn") ) 
		{
			$no = TalkBoard_ReplyArticle($ono, $b, $article_arr);
		} else {
			$no = TalkBoard_InsertArticle($b, $article_arr);			
		}

/*
Warning: mkdir() [function.mkdir]: Permission denied in /home/jbnh.shuvic.co.kr/www/_func/_BoardFunc.php on line 152
-->chmod 707 bf
 */		
		if ($no == -1)
		{
			jserror("등록중 오류가 발생했습니다.","document.location='about:blank'");
			return;
		}
		else
		{
			$upload_file =& $param->getFile('upload_file0');
			if ($upload_file && $upload_file['error'] == 0) 
			{
				if ($upload_file['size'] > 10 * 1024 * 1024)	{// 10M
					jserror("업로드 제한 용량(10MB)이 초과되었습니다.");
				}
				$suffix = preg_replace('!^(.*\.)!', '', $upload_file['name']);
				$dir_name = sprintf("$FILES_BASE/files/".$b."/%s", $no);
				$file_name = $upload_file['name'];
				$rs = SaveFile($upload_file['tmp_name'], $dir_name, $file_name);

				if (!$rs){
					jserror("파일을 저장할 수 없습니다.","document.location='about:blank'");
				}
				else
				{
/*
Fatal error: Call to a member function queryUpdate() on a non-object in /home/jbnh.shuvic.co.kr/www/html/bbs/x.php on line 94
 * */					
					$array['upload_file0']		= $upload_file['name'];

					$result = TalkBoard_UpdateArticle($b, $no, &$array);
					
					
					if (!$result){
						jserror("파일업로딩중 오류가 발생했습니다.","document.location='about:blank'");
						return;
					}
					else{
						cacheControl(0);
						// 출력 버퍼를 비운다.
						ob_clean();
						$basic_param['no']			= $no;
						jserror("등록되었습니다.","parent.location.replace('v.html?" . encodeParamArray($basic_param) . "')");
					}
				}
			}
			else
			{
				cacheControl(0);
				// 출력 버퍼를 비운다.
				ob_clean();
				$basic_param['no']			= $no;
				jserror("등록되었습니다.","parent.location.replace('v.html?" . encodeParamArray($basic_param) . "')");
			}
		}
	}
	
	//삭제
	if ($mode == "d")
	{
		if ($no == "")
		{
			jserror("글번호가 없습니다.","document.location='about:blank'");
			return;
		}
		else
		{
			$row =& TalkBoard_GetArticle($b, $no);
			if ( $row == null )
			{
				jserror("등록된 글이 없습니다.","document.location='about:blank'");
				return;
			}
			else
			{
				
				$tid		= $row->getNumber('tid');
				$todr		= $row->getString('todr');
				$tdpt		= $row->getString('tdpt');				
				
				$isOwner = IsOwner(GetCurrentUser(), $row->getString('writer_id'));
				
				if ( $isOwner )
					$result = TalkBoard_DeleteArticle($b, $no, $tid, $todr, $tdpt, null);

				if (!$result)
				{
					if ( !$isOwner )
						jserror("본인글이 아닙니다.","document.location='about:blank'");
					else
						jserror("삭제중 오류가 발생했습니다","document.location='about:blank'");
					return;
				}
				else
				{
					cacheControl(0);
					// 출력 버퍼를 비운다.
					ob_clean();
					$dir_name = sprintf("$FILES_BASE/files/".$b."/%s", $no);
					RemoveDirFiles($dir_name,true);
					jserror("삭제되었습니다.","parent.location.replace('l.html?" . encodeParamArray($basic_param) . "')");
				}
			}
		}
	}
	
	
	
	//수정
	if ($mode == "u")
	{
		if ($no == "")
		{
			jserror("글번호가 없습니다.","document.location='about:blank'");
			return;
		}

		if ($writer_name == "")
		{
			jserror("작성자가 입력되지 않았습니다.","document.location='about:blank'");
			return;
		}

		if ($subject == "")
		{
			jserror("제목이 입력되지 않았습니다.","document.location='about:blank'");
			return;
		}

		$subject				= $param->getString('subject');
		$writer_name			= $param->getString('writer_name');
		$writer_password	= $param->getString('writer_password');
		$content				= $param->getString('content');


		$article_arr = array(
			'writer_name'		=> $writer_name,
			'subject'				=> $subject,
			'content'			=> $content,
		);

		if ( !isAdmin() ) $article_arr['writer_password'] = $writer_password;


		$row =& TalkBoard_GetArticle($b, $no);
		if ( $row == null )
		{
			jserror("등록된 글이 없습니다.","document.location='about:blank'");
			return;
		}

		$table_name = TalkBoard_GetTableName($b);
		$db = GetDefaultConnection();

		$db->begin();

		$isOwner = IsOwner(GetCurrentUser(), $row->getString('writer_id'));
		if ( $isOwner )
			$result = $db->queryUpdate($table_name, $article_arr, " (no = $no)");

		if (!$result)
		{
			$db->rollback();
			if ( !$isOwner )
				jserror("본인글이 아닙니다.","document.location='about:blank'");
			else
				jserror("수정중 오류가 발생했습니다","document.location='about:blank'");
			return;
		}
		else
		{
			$upload_file =& $param->getFile('upload_file0');
			
			if ($upload_file && $upload_file['error'] == 0)
			{
				if ($upload_file['size'] > 10 * 1024 * 1024)	{// 10M
					jserror("업로드 제한 용량(10MB)이 초과되었습니다.");
				}
				$suffix = preg_replace('!^(.*\.)!', '', $upload_file['name']);
				$dir_name = sprintf("$FILES_BASE/files/".$b."/%s", $no);
				$file_name = $upload_file['name'];
				
				
				//기존 첨부파일 삭제
				RemoveDirFiles($dir_name,false);
				
				$rs = SaveFile($upload_file['tmp_name'], $dir_name, $file_name);
				if (!$rs){
					jserror("파일을 저장할 수 없습니다.","document.location='about:blank'");
				}
				else
				{
					$array['upload_file0']		= $upload_file['name'];
					$result = $db->queryUpdate($table_name, $array, "(no = $no)");
				}
			}

			if (!$result){
				$db->rollback();
				jserror("파일업로딩중 오류가 발생했습니다.","document.location='about:blank'");
				return;
			}
			else
			{
				$db->commit();
				cacheControl(0);
				// 출력 버퍼를 비운다.
				ob_clean();
				$basic_param['no']			= $no;
				jserror("수정되었습니다.","parent.location.replace('v.html?" . encodeParamArray($basic_param) . "')");
			}
		}
	}
?>	
</body>
</html>